#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;

class Solution{
    public:
    //my ans
    string ans1(string strs){
        vector<string> words;
        string split = " ", temp;
        stringstream ss;
        ss << strs;
        while(ss >> temp){
            words.push_back(temp);
        }
        int length = words.size();
        string out_temp = "";
        for(int i = 0; i != length-1; i++){
            out_temp = out_temp + words[length-i-1] + " ";
        }
        out_temp = out_temp + words[0];

        return out_temp;
    }

    string ans2(string strs){
        reverse(strs.begin(), strs.end());

        int n = strs.size();
        int idx = 0;
        for (int start = 0; start < n; ++start) {
            if (strs[start] != ' ') {
                if (idx != 0) strs[idx++] = ' ';
                int end = start;
                while (end < n && strs[end] != ' ') strs[idx++] = strs[end++];
                reverse(strs.begin() + idx - (end - start), strs.begin() + idx);
                start = end;
            }
        }
        strs.erase(strs.begin() + idx, strs.end());
        return strs;
    }

};

void test(){
    Solution s;
    string strs = "the sky is blue";
    // string ret = s.ans1(strs);
    string ret = s.ans2(strs);
    cout << ret << endl;
}

int main(void){
    test();

    return 0;
}